<?php
namespace Tbf\Bundle\ImageBundle;
use Tbf\Bundle\UploadBundle\BlobInterface;
use Tbf\Bundle\UploadBundle\error;
use Tbf\Bundle\UploadBundle\MemoryBlob;
use Tbf\Bundle\UploadBundle\TransformerInterface;

/**
 * 重新修改图片尺寸为某个尺寸,顺便修改图片质量
 * 1.只会把图缩小              如果需要放大,请用浏览器放大..可以提高性能
 * 2.不改变图片比例            改变图片比例,图片会失真..
 * 3.满足1和2的基础上,输出尺寸宽最大值为 $need_width
 * 4.满足1和2的基础上,输出尺寸高最大值为$need_height
 *
 */
class ResizePictureTransformer implements TransformerInterface
{
    protected $need_width;
    protected $need_height;
    protected $quality;
    public function __construct($need_width,$need_height,$quality)
    {
        $this->need_height = $need_height;
        $this->need_width = $need_width;
        $this->quality = $quality;
    }
    /**
     * @param  BlobInterface $input
     * @return BlobInterface $output
     * @return error
     */
    public function transform(BlobInterface $input)
    {
        list($content,$err) = $input->getContent();
        if ($err!==null) {
            return array(null,$err);
        }
        $input_image = \WideImage::loadFromString($content);
        $output_size = $this->countResizeSize(array(
            'input_width'=>$input_image->getWidth(),
            'input_height'=>$input_image->getHeight(),
            'need_width'=>$this->need_width,
            'need_height'=>$this->need_height
        ));
        $output_image = $input_image->resize(
            $output_size['width'],$output_size['height'],'fill');
        $after_content = $output_image->asString('jpg',$this->quality);

        return array(new MemoryBlob($after_content,'jpg'),null);
    }

    public function countResizeSize($data)
    {
        //只会把图缩小
        if ($data['input_width']<=$data['need_width']
            && $data['input_height'] <= $data['need_height']
        ) {
            return array(
                'width'=> $data['input_width'],
                'height'=> $data['input_height'],
                'need_resize'=>false
            );
        }
        $input_ratio = $data['input_height'] / $data['input_width'];
        // 1.宽大于需要的宽,按比例缩放后 高小于需要的高
        // 2.宽大于需要的宽,按比例缩放后高大于需要的高,(依照宽重新计算高度)
        // 3.宽小于需要的宽,高大于需要的高.
        // ---没有其他情况了
        if ($data['input_width'] > $data['need_width']) {
            $width = $data['need_width'];
            $height = $width * $input_ratio;
            if ($height > $data['need_height']) {
                //输入的两个尺寸为最大尺寸
                $height = $data['need_height'];
                $width = $height / $input_ratio;

                return array(
                    'width'=> $width,
                    'height'=> $height,
                    'need_resize'=>true
                );
            }

            return array(
                'width'=> $width,
                'height'=> $height,
                'need_resize'=>true
            );
        }
        $height = $data['need_height'];
        $width = $height / $input_ratio;

        return array(
            'width'=>$width,
            'height' => $height,
            'need_resize'=>true
        );
    }
}
